import VueRouter from "vue-router";
import Vue from "vue";
import { checkLogin, getLoginInfo } from "@/api/hr";
import { Message } from "element-ui";
Vue.use(VueRouter)
const routes = [
    { path: "/", redirect: "/login" },
    { name: "login", path: "/login", component: () => import("@/views/LoginPage.vue") },
    {name:"companyCheck",path:"/companyCheck",component:()=>import("@/views/companyCheck.vue")},
    {
        name: "main", path: "/main", component: () => import("@/views/MainPage.vue"),
        children:
            [
                { path: "/main", redirect: "home" },
                //首页
                { name: "home", path: "home", component: () => import("@/views/homePages/HomePage.vue") },

                
                
                { name: "recruit", path: "recruit", component: () => import("@/views/recruitPages/RecruitInfoPage.vue") },
                {
                    name: "interact", path: "interact", component: () => import("@/views/mainPages/InteractPage.vue"), children: [
                        { name: "chatUser", path: 'chatUser/:id', component: () => import("@/views/mainPages/ChatPage.vue") }
                    ]
                },
                {
                    name: "mine", path: "mine", component: () => import("@/views/mainPages/PersonalPage.vue"),
                    children:
                        [
                            { name: "pay", path: "pay/num/:num/duration/:duration", component: () => import("@/views/PayPage.vue") },
                            { name: "blackList", path: "black", component: () => import("@/views/mainPages/personal/Blacklist.vue") },
                            { name: "personalInfo", path: "personalInfo", component: () => import("@/views/mainPages/personal/MyInfo.vue") },
                            { name: "collection", path: "collection", component: () => import("@/views/mainPages/personal/Mycollection.vue") },
                            { name: "meeting", path: "meeting", component: () => import("@/views/mainPages/personal/MeetingList.vue") },
                            { name: "AIchat", path: "AIchat", component: () => import("@/views/mainPages/personal/AIchat.vue") },
                            { name: "submitHR", path: "submitHR", component: () => import("@/views/mainPages/personal/SubHr.vue") },
                            {
                                name: "service", path: "service", component: () => import("@/views/mainPages/personal/ManService.vue"),
                                children: [
                                    { name: "chat", path: 'chat/:id', component: () => import("@/views/mainPages/personal/ManChat.vue") }
                                ]
                            },
                        ]
                }
            ]
    }
]



const router = new VueRouter({ routes })

import store from "@/store";
import NProgress from "@/utils/nprogress";

router.beforeEach((to, from, next) => {
    NProgress.start()
    let whiteList = ["/","/login"]
    if (whiteList.indexOf(to.fullPath) != -1) {
        next();
    } else {
        checkLogin().then(res => {
            store.dispatch("setSelfInfo", {"hrId":res.data})
            if (res.code == 200) {
                if (store.state.hrName == null) {
                    getLoginInfo().then(response => {
                        store.dispatch("setSelfInfo", response.data)
                    })
                }
                next()
            } else {
                Message.error(res.msg)
                next("/login")
            }
        })
    }
})
router.afterEach((to, from) => {
    NProgress.done()
})

export default router